import sqlite3
import os

class Dosqlite:
    def __init__(self):
        if not os.path.exists('data'):
            os.mkdir('data')
            self.connect = sqlite3.connect("data/testsqlite.data")
            self.run("CREATE TABLE request (  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  fid text,  method TEXT,  url TEXT,  header TEXT,  data TEXT DEFAULT '',  'context-type' TEXT,  time real)")
            self.run("CREATE TABLE response (  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,url TEXT,   fid text,  code TEXT,  msg TEXT,  header TEXT,  text TEXT,  'content-type' TEXT,'save-type' TEXT,  time real)") 
            self.run("CREATE TABLE element (  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  fid text,  method TEXT,  url TEXT,  header TEXT,  data TEXT DEFAULT '',  'context-type' TEXT,  time real)")

        else:
            if os.path.exists("data/testsqlite.data"):
                pass
                self.connect = sqlite3.connect("data/testsqlite.data")
                # self.run("DELETE FROM  request ")
                # self.run("DELETE FROM response")
            else:
                self.connect = sqlite3.connect("data/testsqlite.data")
                self.run("CREATE TABLE request (  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  fid text,  method TEXT,  url TEXT,  header TEXT,  data TEXT DEFAULT '',  'context-type' TEXT,  time real)")
                self.run("CREATE TABLE response (  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,url TEXT,  fid text,  code TEXT,  msg TEXT,  header TEXT,  text TEXT,  'content-type' TEXT,'save-type' TEXT,  time real)") 
                self.run("CREATE TABLE element (  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  fid text,  method TEXT,  url TEXT,  header TEXT,  data TEXT DEFAULT '',  'context-type' TEXT,  time real)")
                
        
    def run(self,sql):
        # raise Exception(sql)
        cursor = self.connect.cursor()
        # sql = "CREATE TABLE student(id INT PRIMARY KEY, name VARCHAR(10) not null)"
        try :
            cursor.execute(sql)
        except Exception as e:
            cursor.close()
            raise e
        
        cursor.close()
        self.connect.commit()
    def close(self):
        self.connect.close()




# connect.close()
